package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @auther ljw
 * @date 2021/6/29 19:05
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);
        Integer roleId = role.getId();
        setRoleAndPermission(roleId,permissionIds);
        setRoleAndMenu(roleId,menuIds);
    }

    @Override
    public void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("permission_id", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    @Override
    public void setRoleAndMenu(Integer roleId, Integer[] menuIds) {
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("menu_id", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> roles = roleDao.selectByCondition(queryString);
        long total = roles.getTotal();
        List<Role> result = roles.getResult();
        return new PageResult(total,result);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findPermissionIdsByUserId(Integer id) {
        return roleDao.findPermissionIdsByUserId(id);
    }

    @Override
    public List<Integer> findMenuIdsByUserId(Integer id) {
        return roleDao.findMenuIdsByUserId(id);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        Integer roleId = role.getId();
        roleDao.deleteAssociation(roleId);
        roleDao.deleteAssociation01(roleId);
        setRoleAndPermission(roleId,permissionIds);
        setRoleAndMenu(roleId,menuIds);
        roleDao.edit(role);
    }

    @Override
    public void delete(Integer id) {
        roleDao.deleteAssociation(id);
        roleDao.deleteAssociation01(id);
        roleDao.delete(id);
    }
}
